package com.longge;

import java.util.Arrays;

/**
 * @创建人 xinglongge
 * @创建时间 2021/11/29
 * @描述
 */
public class AddNegabinary {
    public int[] addNegabinary(int[] arr1, int[] arr2) {
        int i = arr1.length - 1;
        int j = arr2.length - 1;
        int len = Math.max(i + 1, j + 1) + 2;
        int[] res = new int[len];
        int c = 0;
        int k = len - 1;
        while (i >= 0 || j >= 0 || c != 0) {
            if (i >= 0) c += arr1[i];
            if (j >= 0) c += arr2[j];
            res[k--] = Math.abs(c) % 2;
            if (c < 0) c = 1;
            else if (c > 1) c = -1;
            else c = 0;
            i--;
            j--;
        }

        //寻找第一位不为0的数
        int index = 0;
        while (index < len - 1 && res[index] == 0) {
            index++;
        }
        return Arrays.copyOfRange(res, index, len);

    }

    public static void main(String[] args) {
        new AddNegabinary().addNegabinary(new int[]{1,1,1,1,1}, new int[]{1,0,1});
    }
}
